package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


//套餐

@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;
    //套餐列表
    @Override
    public Page<Setmeal> findByPage(Integer pageNum, Integer pageSize, String name) {
        //1.套餐条件分页查询
        //1-1构建条件
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        //模糊查询

        //1-2开启分页
        Page<Setmeal> page = new Page<>(pageNum,pageSize);
        //1-3mapper查询
        page = setmealMapper.selectPage(page, qw);
        //遍历套餐列表
        for (Setmeal setmeal : page.getRecords()) {
          //2.根据category_id 查询并封装分类名称
            Category category = categoryMapper.selectById(setmeal.getCategoryId());
            setmeal.setCategoryName(category.getName());
            //3.根据id查询并封装菜品列表
            LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
           //对比中间表跟套餐表的id
            wrapper.eq(SetmealDish::getSetmealId,setmeal.getId());

            List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);
            //调用setmeal
            setmeal.setSetmealDishes(setmealDishList);
        }
        //4.返回组装后的数据
           return page;
    }

    //新增套餐
    @Override
    public void save(Setmeal setmeal) {
       //1.新增套餐
       setmealMapper.insert(setmeal);
       //2.遍历前端提交套餐菜品列表
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishList) {
            //关联套餐id
            setmealDish.setSetmealId(setmeal.getId());
            //保存中间表
            setmealDishMapper.insert(setmealDish);
        }
    }
    //删除套餐
    @Override
    public void deleteById(List<Long> ids) {
        //先查询套餐状态,释放是启售
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId,ids); //id in(1,3,5)打比方里面是这几个想删除的套餐
        qw.eq(Setmeal::getStatus,1); //判断是否为启售 status=1
        Integer count = setmealMapper.selectCount(qw);
        if (count>0){
            throw new CustomException("改套餐中有启售状态,先停售再删除");
        }
        //2.删套餐
        setmealMapper.deleteBatchIds(ids);
        //3.删除套餐内菜品中间表
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishMapper.delete(wrapper);

    }

    //启售停售
    @Override
    public void updateStatus(Integer status, List<Long> ids) {
      //创建表达式
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        //条件对象
        qw.in(Setmeal::getId,ids);
        //创建实体
        Setmeal setmea1 = new Setmeal();
        setmea1.setStatus(status);
        setmealMapper.update(setmea1,qw);
    }

    //回显
    @Override
    public Setmeal findById(Long id) {
        //1.先查套餐
        Setmeal setmeal = setmealMapper.selectById(id);
        //根据套餐id查询菜品
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,id);
        //2-2调用mapper
        List<SetmealDish> dishList = setmealDishMapper.selectList(qw);
        //3.将菜品设置到套餐
        setmeal.setSetmealDishes(dishList);
        //4.返回套餐
        return setmeal;
    }

    //修改套餐
    @Override
    public void updateById(Setmeal setmeal) {
        //修改套餐基本信息
        setmealMapper.updateById(setmeal);
        //2.根据套餐id修改原来的菜品
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,setmeal.getId());
        //删除
        setmealDishMapper.delete(qw);
        //3.遍历新增菜品
        for (SetmealDish dish : setmeal.getSetmealDishes()) {
            //关联套餐id
            dish.setSetmealId(setmeal.getId());
            //菜品新增
            setmealDishMapper.insert(dish);
        }
    }

    //套餐展示
    @Override
    public List<Setmeal> findList(Long categoryId, Integer status) {
        //1.构建条件
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId,categoryId);//分类
        qw.eq(Setmeal::getStatus,status);//启售停售
        return setmealMapper.selectList(qw);
    }
    //套餐菜品图片
    @Override
    public List<Dish> getmealDish(Long id) {
        //1. 从中间表获取菜品信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);

        //2. 创建一个集合用于封装返回菜品信息
        List<Dish> dishList = new ArrayList<>();

        //3. 遍历得到菜品列表
        for (SetmealDish setmealDish : setmealDishList) {
            //根据菜品id从菜品表查询菜品详情
            Dish dish = dishMapper.selectById(setmealDish.getDishId());
            dish.setCopies(setmealDish.getCopies());//设置份数
            dishList.add(dish);//添加到返回集合
        }

        //4. 返回
        return dishList;
    }


    }


